<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>{$setting.webname} - 添加管理员</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="/static/admin/layui/css/layui.css" rel="stylesheet">
    <!--<link rel="shortcut icon" href="/favicon.ico">-->
    <link href="/static/admin/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="/static/admin/css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <link href="/static/admin/css/animate.css" rel="stylesheet">
    <link href="/static/admin/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="/static/admin/css/plugins/iCheck/custom.css" rel="stylesheet">
    <style>
        .menuTitle{
            margin-top:10px;
            line-height: 35px;
            background:#cccccc;
            color:#000;
            padding-left:15px;

        }
        label{
            padding-left:0px!important;
            padding-right: 20px!important;
        }
    </style>


</head>

<body class="gray-bg">
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2>添加管理员</h2>
        <ol class="breadcrumb">
            <li>
                <a href="{:url('Index/admin')}">主页</a>
            </li>
            <li>
                <strong>添加管理员</strong>
            </li>
        </ol>
    </div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">

            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑管理员信息</h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户名：</label>
                            <div class="col-sm-4">
                                <input type="text" placeholder="请输入用户名" id="username" class="form-control">
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">性别：</label>
                            <div class="col-sm-4">
                                <div class="radio i-checks" id="sex">
                                    <label><input type="radio" name="sex" value="1" class="status" checked>  女</label>
                                    <label><input type="radio" name="sex" value="0" class="status"> 男</label>
                                </div>
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">年龄：</label>
                            <div class="col-sm-4">
                                <input type="number" placeholder="请输入年龄" id="age" class="form-control">
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">身份证号：</label>
                            <div class="col-sm-4">
                                <input type="text" placeholder="请输入身份证号" id="cardid" class="form-control">
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" >在职状态：</label>
                            <div class="col-sm-4" id="poststatus">
                                <div class="radio i-checks">
                                    <label><input type="radio" name="poststatus" value="1" class="status" checked>  在职</label>
                                    <label><input type="radio" name="poststatus" value="2" class="status"> 离职</label>
                                    <label><input type="radio" name="poststatus" value="3" class="status"> 停职</label>
                                </div>
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" >职务类型：</label>
                            <div class="col-sm-4" id="posttype">
                                <div class="radio i-checks">
                                    <label><input type="radio" name="posttype" value="1" class="status" checked>  全职</label>
                                    <label><input type="radio" name="posttype" value="2" class="status"> 兼职</label>
                                </div>
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">密码：</label>
                            <div class="col-sm-4">
                                <input type="password" placeholder="请输入密码" id="password" class="form-control"/>
                            </div>
                            <div class="col-sm-6"></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">确认密码：</label>
                            <div class="col-sm-4">
                                <input type="password" placeholder="请再次输入密码" id="password2" class="form-control">
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">手机号码：</label>
                            <div class="col-sm-4">
                                <input type="text" placeholder="请输入手机号码" id="phone" class="form-control">
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">邮箱：</label>
                            <div class="col-sm-4">
                                <input type="text" placeholder="请输入邮箱地址" id="email" class="form-control">
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">联系地址：</label>
                            <div class="col-sm-4">
                                <input type="text" placeholder="请输入联系地址" id="map" class="form-control">
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">角色：</label>
                            <div class="col-sm-4">
                                <select class="form-control m-b rid" name="rid" id="r_id">
                                {foreach name="rols" item="vo"}
                                <option value="{$vo.r_id}" data-pid="{$vo.pid}">{$vo.r_name}</option>
                                {/foreach}
                                </select>
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">上级领导：</label>
                            <div class="col-sm-4">
                                <select class="form-control m-b" name="pid" id="pid">
                                {foreach $admins as $vo}
                                <option value="{$vo.id}">{$vo.username}</option>
                                {/foreach}
                                </select>
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态：</label>
                            <div class="col-sm-4">
                                <div class="radio i-checks" id="status">
                                    <label><input type="radio" name="status" value="1" class="status" checked>  可用</label>
                                    <label><input type="radio" name="status" value="2"  class="status">  不可用</label>
                                </div>
                            </div>
                            <div class='col-sm-6'></div>
                        </div>

                        <!-- 上传图片 -->
                        <div class="form-group">
                            <label class="col-sm-2 control-label">头像：</label>
                            <div class="col-sm-4">
                                <button type="button" class="btn btn-info" id="test1">修改图片</button>
                                <div class="layui-upload-list">
                                    <img class="layui-upload-img" id="demo1" src="">
                                    <p id="demoText"></p>
                                </div>
                            </div>
                            <div class='col-sm-6'></div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-9">
                                <button class="btn btn-sm btn-info" type="button" id="adds">保 存</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 全局js -->
<script src="/static/admin/js/jquery.min.js?v=2.1.4"></script>
<script src="/static/admin/js/bootstrap.min.js?v=3.3.6"></script>
<!-- 自定义js -->
<script src="/static/admin/js/content.js?v=1.0.0"></script>
<script src="/static/admin/js/jquery.min.js"></script>
<script src="/static/admin/layui/layui.js"></script>
<!--icheck-->
<script src="/static/admin/js/plugins/iCheck/icheck.min.js"></script>

<script>

    /*
     * icheck
     */

    //当角色发送变化的时候查询当前角色的领导
    $("#r_id").change(function(){
        id = $("#r_id").val();
        pids = $("#r_id option[value="+id+"]").attr("data-pid");
        $.post("{:url('Admins/getld')}",{pid:pids},function(data){
            $("#pid").html(data);
        });
    });
    //单选和复选框
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });
    //layui
    layui.use('upload', function(){ //独立版的layer无需执行这一句
        var $ = layui.jquery, layer = layui.layer,upload=layui.upload;//独立版的layer无需执行这一句
        var photo ="{$data.photo}";
        //点击提交
        $("#adds").click(function(){

            //获取状态
            var status = $("#status input:radio[class='status']");
            var statusVal  = "";
            for(i in status){
                if(status.eq(i).is(":checked")) {
                    var statusVal =status.eq(i).val();
                }
            }
            //获取性别
            var sex = $("#sex input:radio");
            var sexVal  = "";
            for(i in sex){
                if(sex.eq(i).is(":checked")) {
                    var sexVal =sex.eq(i).val();
                }
            }
            //获取职位状态
            var poststatus = $("#poststatus input:radio");
            var poststatusval  = "";
            for(i in poststatus){
                if(poststatus.eq(i).is(":checked")) {
                    var poststatusval =poststatus.eq(i).val();
                }
            }
            //获取职位类型
            var posttype = $("#posttype input:radio");
            var posttypeval  = "";
            for(i in posttype){
                if(posttype.eq(i).is(":checked")) {
                    var posttypeval =posttype.eq(i).val();
                }
            }

            if($("#username").val() == ""){
                layer.msg("请输入用户名",function(){
                    $("#username").focus();
                });
            }else{
                if($("#age").val() == ""){
                    layer.msg("请输入年龄",function(){
                        $("#username").focus();
                    });
                }else {
                    
                    if(false){ //原本为判断身份证是否为空
                        layer.msg("请输入身份证号码",function(){
                            $("#username").focus();
                        });
                    }else {
                        // if(!cidInfo($("#cardid").val())){ //判断身份证号码是否正确
                        if(false){
                            layer.msg("请输入正确的是身份证号码",function(){
                                $("#username").focus();
                            });
                        }else {
                            //判断密码是否修改
                            if ($("#password").val() != "") {
                                if ($("#password2").val() != "") {
                                    if ($("#password2").val() == $("#password").val()) {
                                        //判断电话号码是否为空
                                        if ($("#phone").val() == "") {
                                            layer.msg("请输入电话号码");
                                        } else {
                                            //判断是否是手机号码格式
                                            var phonepreg = /^1(3|5|7|8|9)\d{9}$/;
                                            if (!phonepreg.test($("#phone").val())) {
                                                layer.msg("请输入正确手机号码");
                                            } else {
                                                //判断邮箱是否为空
                                                if ($("#email").val() == "") {
                                                    layer.msg("请输入邮箱");
                                                } else {
                                                    //判断邮箱是邮箱格式
                                                    var emailPreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                                                    if (emailPreg.test($("#email").val())) {
                                                        if ($("#map").val() == "") {
                                                            layer.msg("请输入联系地址");
                                                        } else {

                                                            //提交ajax添加用户
                                                            $.post("{:url('Admins/adds')}", {
                                                                username: $("#username").val(),
                                                                password: $("#password").val(),
                                                                status: statusVal,
                                                                phone: $("#phone").val(),
                                                                email: $("#email").val(),
                                                                photo: photo,
                                                                map: $("#map").val(),
                                                                sex: sexVal,
                                                                age: $("#age").val(),
                                                                cardid: $("#cardid").val(),
                                                                poststatus: poststatusval,
                                                                posttype: posttypeval,
                                                                roles_id: $("#r_id").val(),
                                                                pid: $("#pid").val(),

                                                            }, function (data) {
                                                                layer.msg(data.msg, {time: 2000}, function () {
                                                                    if (data.url) {
                                                                        //parent.location.reload();
                                                                        window.location.href = data.url;
                                                                    }
                                                                });
                                                            });

                                                        }
                                                    } else {
                                                        layer.msg("请输入正确邮箱");
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        layer.msg("两次密码不一致");
                                    }
                                } else {
                                    layer.msg("请再次输入密码");

                                }
                            } else {
                                layer.msg("请输入密码");
                            }
                        }
                    }
                }
            }

        });
        //验证身份证是否合法
        var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州"
            ,53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
        function cidInfo(sId){
            var iSum=0
            var info=""
            if(!/^\d{17}(\d|x)$/i.test(sId))return false;
            sId=sId.replace(/x$/i,"a");
            if(aCity[parseInt(sId.substr(0,2))]==null)return false;
            sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
            var d=new Date(sBirthday.replace(/-/g,"/"))
            if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return false;
            for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
            if(iSum%11!=1)return false;
            return true;
        }
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: "{:url('Admins/upload')}"
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code == 0){
                    return layer.msg('上传失败');
                }else if(res.code){
                    //上传成功
                    photo = res.photo;
                    return layer.msg('上传成功');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

    });

</script>



</body>

</html>
